﻿using AutoMapper;
using MD5Hash;
using MediatR;
using RBAC_Class.Read.API.Application.Command;
using RBAC_Class.Read.API.Dto;
using RBAC_Class.Domain;
using RBAC_Class.ErrorCode;
using RBAC_Class.Infrastructrue.Interfaces;

namespace RBAC_Class.Read.API.Application.CommandHandler
{
    public class LoginCommandHandler:IRequestHandler<LoginCommand,APIResult<List<UserDto>>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<UserRole> userRoleRep;
        private readonly IMapper mapper;
        private readonly ILogger<LoginCommandHandler> logger;

        public LoginCommandHandler(IBaseRepository<User> userRep, IBaseRepository<Role> roleRep, IBaseRepository<UserRole> userRoleRep, IMapper mapper, ILogger<LoginCommandHandler> logger)
        {
            this.userRep = userRep;
            this.roleRep = roleRep;
            this.userRoleRep = userRoleRep;
            this.mapper = mapper;
            this.logger = logger;
        }

        public Task<APIResult<List<UserDto>>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = from a in userRep.GetAll()
                           join b in userRoleRep.GetAll()
                           on a.UserID equals b.UserID
                           join c in roleRep.GetAll()
                           on b.RoleID equals c.RoleID
                           where a.UserName == request.UserName
                           select new UserDto
                           {
                               UserID = a.UserID,
                               UserName = a.UserName,
                               Password = a.Password,
                               RoleID = c.RoleID,
                               RoleName = c.RoleName,
                               Status = a.Status,
                               CreateTime = a.CreateTime
                           };
                if (list == null)
                {
                    return Task.FromResult(new APIResult<List<UserDto>>
                    {
                        Code = ResultCode.Fail,
                        Message = "用户不存在"
                    });
                }
                if (list.FirstOrDefault().Password != request.Password.GetMD5())
                {
                    return Task.FromResult(new APIResult<List<UserDto>>
                    {
                        Code = ResultCode.Fail,
                        Message = "密码错误"
                    });
                }

                return Task.FromResult(new APIResult<List<UserDto>>
                {
                    Code = ResultCode.Success,
                    Message = "登录成功",
                    Data = list.ToList()
                });
            }
            catch (Exception ex)
            {
                logger.LogError("登录出错", ex);
                throw;
            }
            
        }
    }
}
